package com.imooc.chart.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.imooc.chart.model.ExportExcelUser;
import com.imooc.chart.util.ImageUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    public void exportUsersToExcel(HttpServletResponse response) {
        // 模拟用户数据
        List<ExportExcelUser> userList = new ArrayList<>();

        userList.add(new ExportExcelUser("张三", "/Users/dengwubo/Pictures/601675749736_pic.jpg"));
        userList.add(new ExportExcelUser("李四", "/Users/dengwubo/Pictures/601675749736_pic.jpg"));

        // 导出操作
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户信息", "用户信息"), ExportExcelUser.class, userList);
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=userInfo.xls");
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
